package com.ms.p2p.service.impl;


import com.ms.common.exception.NoticeException;
import com.ms.common.utils.AppContext;
import com.ms.p2p.dao.MemberAccountDao;
import com.ms.p2p.dao.YueBaoDao;
import com.ms.p2p.entity.account.Account;
import com.ms.p2p.entity.account.YuEbao;
import com.ms.p2p.enums.AccountFLow;
import com.ms.p2p.enums.AccountType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description:活期宝
 */
@Service
@Transactional(readOnly = true)
public class YuEbaoServiceImpl {

	@Autowired
	private YuEbaoLogServiceImpl yuEbaoLogService;
	@Autowired
	private MemberAccountServiceImpl memberAccountService;
	@Autowired
	private MemberAccountRecordServiceImpl accountRecordService;
	@Autowired
	private SettingServiceImpl settingService;
	@Autowired
	private YueBaoDao yuebaoDao;
	@Autowired
	private MemberAccountDao memberAccountDao;

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public int insert(YuEbao map) {
		return yuebaoDao.insert(map);
	}


	public YuEbao findByUserId(int userId) {
		return yuebaoDao.findByUserId(userId);
	}

	/**
	 * 收益计算
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void IncomeDistribution() {
		yuebaoDao.yesDayValidate();
		List<YuEbao> yuEbaos = yuebaoDao.findYueBaoList();
		for (YuEbao yuEbao : yuEbaos) {
			BigDecimal income = (yuEbao.getTotal()).multiply(new BigDecimal(settingService.findValueByCode("yuebao_yield"))).setScale(2, BigDecimal.ROUND_HALF_UP);
			if (income.compareTo(BigDecimal.ZERO) == 1) {
				income(income, yuEbao.getUser_id(), "活期宝收益￥" + income + "。",
						"127.0.0.1");
			}
		}

	}

	/**
	 * 活期宝计算添加收益，加账户总额，记录资金记录和活期宝记录
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void income(BigDecimal money, int userId, String remark, String addIp) {
		yuebaoDao.income(money, userId);
		YuEbao yuEbao = findByUserId(userId);
		yuEbaoLogService.insert(yuEbao, "income", money, remark, addIp);
		memberAccountService.OperAccount(addIp,money,userId,remark, AccountType.income_yuebao.getValue(), AccountFLow.FLOW_IN.getValue());
	}

	/**
	 * 可用金额转入到活期宝 减可用 加活期宝总额 记录活期宝记录，记录资金记录
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public int into(BigDecimal money, int userId, String remark, String addIp) {
		int result = yuebaoDao.addYueBao(money, userId);
		if(result == 0){
			throw new NoticeException("操作失败");
		}
		YuEbao yuEbao = findByUserId(userId);
		result = yuEbaoLogService.insert(yuEbao, "into", money, remark, addIp);
		if(result == 0){
			throw new NoticeException("操作失败");
		}
		memberAccountService.OperAccount(addIp,money,userId,remark, AccountType.into_yuebao.getValue(), AccountFLow.FLOW_INTOYUEBAO.getValue());
		return result;
	}

	/**
	 * 从活期宝撤购到可用，加账户可用，减活期宝总额 记录活期宝记录，记录资金记录
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public int out(BigDecimal money, int userId, String remark, String addIp) {
		int result = yuebaoDao.subYueBao(money, userId);
		if(result == 0){
			throw new NoticeException("操作失败");
		}
		YuEbao yuEbao = findByUserId(userId);
		result = yuEbaoLogService.insert(yuEbao, "out", money, remark, addIp);
		if(result == 0){
			throw new NoticeException("操作失败");
		}
		memberAccountService.OperAccount(addIp,money,userId,remark, AccountType.out_yuebao.getValue(), AccountFLow.FLOW_OUTYUEBAO.getValue());

		return result;
	}
}
